package com.martin.lib_base.ui.screen

import androidx.compose.foundation.gestures.ScrollableState
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxScope
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp

/**
 * @author：孟凡华
 * @date：2023/6/29
 * @desc：
 */

@Composable
fun VerticalGrid(
    // 列表
    items: List<Any>,
    // 每行显示的列数
    columns: Int,
    modifier: Modifier = Modifier,
    // 间距-横向
    paddingHorizontal: Dp = 0.dp,
    // 间距-纵向
    paddingVertical: Dp = paddingHorizontal,
    // item布局
    Item: @Composable BoxScope.(item: Any) -> Unit,
) {
    // 计算行数
    val rows = items.size / columns + (if (items.size % columns == 0) 0 else 1)
    // 空数据不加载
    rows <= 0 && return

    Column(modifier = modifier) {
        for (i in 0 until rows) {
            if (i > 0) {
                Box(
                    modifier = Modifier
                        .height(paddingVertical)
                )
            }
            Row() {
                for (j in 0 until columns) {
                    val index = i * columns + j
                    if (j > 0) {
                        Box(modifier = Modifier.width(paddingHorizontal))
                    }
                    Center(modifier = Modifier.weight(1f)) {
                        if (index < items.size) {
                            Item(items[index])
                        }
                    }
                }
            }
        }
    }
}